/** 
 * COLISION
 *
 * Grupo 4
 * @author Bruno Nova (080509109)
 * @author Pedro Simeão Carvalho (080509157)
 */

// Include de bibliotecas
//#define EMBED
#include <conio.h>
#include <stdio.h>
#ifndef EMBED
	#include <stdlib.h>
#endif
#include <dpmi.h>
#include <string.h>
#include <math.h>

#include "queue.h"

// Variaveis globais
char * video_mem = NULL;
char * image_buffer = NULL;
char * image_buffer_original = NULL;
__dpmi_meminfo video_map;
char * char_table;
int HRES;
int VRES;
Queue queue_keyboard;
_go32_dpmi_seginfo timer0_new_irq;
_go32_dpmi_seginfo timer0_old_irq;
_go32_dpmi_seginfo keyboard_new_irq;
_go32_dpmi_seginfo keyboard_old_irq;
_go32_dpmi_seginfo mouse_old_irq;

extern void t0_isr();
extern void mouse_isr();

// Include de ficheiros

#include "utypes.h"
#include "videog.h"
#include "chtable.c"
#include "timer.h"
#include "jogo.h"
#include "kbc.h"


// Variaveis locais
#define video_mode 101

// Funcoes gerais
MAIN {
	// Arranque do necessario inicial
	char_table = (char*) malloc(sizeof(char)*256*16);
	load_char_table(char_table);
	video_mem = enter_graphics(video_mode, &video_map);
	image_buffer = (char*)malloc(HRES*VRES*sizeof(char));
	image_buffer_original = (char*)malloc(HRES*VRES*sizeof(char));
	reset_screen(BLACK, image_buffer_original);
	queueInit(&queue_keyboard, 100);

	srand((unsigned)time(NULL));
	disable_irq(KBD_IRQ);
	disable_irq(MOUSE_IRQ);
	kbc_init(0);
	enable_irq(MOUSE_IRQ);
	enable_irq(KBD_IRQ);
	// Iniciacao do timer e do jogo
	initTimer0(t0_isr);
	initMouse(mouse_isr);
	initKeyBoard(handler_keyboard);
	
	
	//start();
	menu();

	endKeyBoard();
	//endMouse();
	endTimer0();
	

	//freeJogo();
	free(image_buffer);
	leave_graphics ( &video_map );
	#ifndef EMBED
		return 0;
	#endif
}
